package com.xxxx.keyTest;


import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @program: flink19Test
 * @description: 自己写
 * @author: CoreDao
 * @create: 2021-04-01 11:43
 **/

/**
 * zs man 18 4 80 90
 * lisi woman 20 3 79 60
 */
public class ClassKeyTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //创建一个Student对象， id,性别，年龄，班级，成绩

        DataStreamSource<String> textStream = env.socketTextStream("localhost", 9998);

        KeyedStream<String, String> stringKeyedStream = textStream.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String value) throws Exception {
                return value.split(" ")[5];
            }
        });

        SingleOutputStreamOperator<StudnetInfo> map = textStream.map(new MapFunction<String, StudnetInfo>() {
            @Override
            public StudnetInfo map(String value) throws Exception {
                String[] split = value.split(" ");
                String name = split[0];
                String gender = split[1];
                Integer age = Integer.parseInt(split[2]);
                String className = split[3];
                Integer score1 = Integer.parseInt(split[4]);
                Integer score2 = Integer.parseInt(split[5]);
                return new StudnetInfo(name, gender, age, Tuple2.of(className, Tuple2.of(score1, score2)));
            }
        });
        KeyedStream<StudnetInfo, Tuple> name = map.keyBy("name");
        KeyedStream<StudnetInfo, Tuple> age = map.keyBy("age");
        //表达式描述的都是当前对象对应类的成员属性名称
        KeyedStream<StudnetInfo, Tuple> keyedStream = map.keyBy("classAndScore.f0");

//        KeyedStream<StudnetInfo, String> keyedStream1 = map.keyBy(student -> student.getName());
        /*map.keyBy(new KeySelector<StudnetInfo, String>() {
            @Override
            public String getKey(StudnetInfo value) throws Exception {
                return value.getName();
            }
        }).print();*/

        KeyedStream<StudnetInfo, Tuple> keyedStream2 = map.keyBy("classAndScore.f1.f0");

        keyedStream.reduce(new ReduceFunction<StudnetInfo>() {
            @Override
            public StudnetInfo reduce(StudnetInfo value1, StudnetInfo value2) throws Exception {
                String name = value1.getName() + ":" + value2.getName();
                String gender = value1.getGender() + ":" + value2.getGender();
                int age = value1.getAge() + value2.getAge();
                Tuple2<String, Tuple2<Integer, Integer>> cas1 = value1.getClassAndScore();
                Tuple2<String, Tuple2<Integer, Integer>> cas2 = value2.getClassAndScore();
                Tuple2<String, Tuple2<Integer, Integer>> cas3 = Tuple2.of(cas1.f0 + ":" + cas2.f0, Tuple2.of(cas1.f1.f0 + cas2.f1.f0, cas1.f1.f1 + cas2.f1.f1));

                return new StudnetInfo(name,gender,age,cas3);
            }
        }).print();


        env.execute();
    }
}
